/*
 * interface - consists of script for the main homepage interface
 *
 * Copyright (c) 2008 Varun Kumar Reddy Dodla
 * Licensed under GPL (GPL-LICENSE.txt)
 *
 * $Date: 2008-10-16 
 */

Ext.onReady( function() {

	Ext.QuickTips.init();

	// turn on validation errors beside the field globally
		Ext.form.Field.prototype.msgTarget = 'side';

		// lets build our own form
		var urlForm = new Ext.form.FormPanel( {
			method :'post',
			frame :true,
			title :'Form',
			defaults : {
				width :230
			},
			defaultType :'textfield',
			items : [ {
				fieldLabel :'Enter URL Here',
				name :'url',
				vtype: 'url',
				allowBlank :false
			} ],
			buttons : [ {
				text :'Load',
				handler : function() {
					if (urlForm.getForm().isValid()) {
						//log.message('Got a valid url.');
						getUrl(urlForm.getForm().findField('url').getValue());
					}
					;
				}
			}, {
				text :'Clear',
				handler : function() {
					urlForm.getForm().findField('url').reset();
				}
			}, {
				text :'Test',
				handler : function() {
					conductTests(true);
				}
			} ]
		});
		
		//finally the main view
		var viewport = new Ext.Viewport( {
			layout :'border',
			items : [ {
				region :'north',
				split :true,
				height :100,
				minSize :100,
				maxSize :200,
				collapsible :true,
				margins :'0 0 0 0',
				items : urlForm
			}, {
				region :'south',
				contentEl :'south',
				split :true,
				height :150,
				minSize :100,
				maxSize :200,
				collapsible :true,
				title :'Log',
				margins :'0 0 0 0',
				autoScroll :true
			}, {
				region :'east',
				title :'DOM Panel',
				collapsible :true,
				split :true,
				width :250,
				minSize :200,
				maxSize :400,
				layout :'fit',
				margins :'0 5 0 0',
				contentEl :'east',
				autoScroll :true

			}, {
				region :'west',
				title :'TODOs',
				split :true,
				width :175,
				minSize :175,
				maxSize :400,
				collapsible :true,
				margins :'0 0 0 5',
				contentEl :'west',
				collapsed: true
			}, {
				region :'center',
				contentEl :'center',
				split :true,
				height :100,
				minSize :100,
				maxSize :200,
				collapsible :false,
				title :'URL Panel',
				margins :'0 0 0 0'
			} ]
		});
		
		
		//the form tree
		formTree = new Ext.tree.TreePanel({
			id: 'formTree',
	        useArrows:true,
	        autoScroll:true,
	        animate:true,
	        enableDD:false,
	        containerScroll: true,

	        root: {
	            text: 'Forms',
	            id:'formTreeRoot',
	            leaf: true
	        }
	    });

	    // render the tree
	    formTree.render('east');

	    //method to show error message
	    showErrorBox = function (sTitle, sMsg) {
	    	if(Ext.MessageBox.isVisible())
	    		Ext.MessageBox.hide();
	    	
	    	Ext.MessageBox.show({
	    		title: sTitle,
	    		msg: sMsg,
	    		buttons: Ext.MessageBox.OK,
	    		icon: Ext.MessageBox.ERROR
	    	});
	    }

	});


function showDependencies() {


//Generic fields array to use in both store defs.
var fields = [
   {name: 'formIdx', mapping : 'formIdx'},
   {name: 'testAttr', mapping : 'testAttr'},
   {name: 'actualAttr', mapping : 'actualAttr'},
   {name: 'comments', mapping : 'comments'}
];


// create the data store
var firstGridStore = new Ext.data.JsonStore({
    fields : fields,
	data   : dependencies
});

//Column Model shortcut array
var cols = [
	{header: "Form", width: 50, sortable: true, dataIndex: 'formIdx',  menuDisabled: true },
	{header: "Test Attr", width: 100, sortable: true, dataIndex: 'testAttr',  menuDisabled: true},
	{header: "Affected Attr", width: 100, sortable: true, dataIndex: 'actualAttr',  menuDisabled: true},
	{id: "comments", header: "Comments", sortable: false, dataIndex: 'comments',  menuDisabled: true}
];

// declare the source Grid
var firstGrid = new Ext.grid.GridPanel({
    store    		: firstGridStore,
    columns  		: cols,
    autoExpandColumn: 'comments',
    title    : 'This table shows the dependencies so far',
    autoHeight : true
});

var win = new Ext.Window({
    title    : 'Dependencies',
    closable : true,
    width    : 650,
    height   : 350,
    items    : [firstGrid],
    autoScroll: true,
    maximizable: true,
    modal: true
});

//message
Ext.MessageBox.hide();

//firstGrid.render('west');
win.show();

}